package com.hy.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hy.ruiji.common.R;
import com.hy.ruiji.common.Status;
import com.hy.ruiji.enyity.ShoppingCart;
import com.hy.ruiji.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart 选购菜品或套餐的信息
     * @return 返回添加的菜品或者套餐
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户id
        Long userId = Status.getCurrentId();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        //判断当前套餐和菜品是否在购物车中
        Long dishId = shoppingCart.getDishId();//获取菜品id
        if(dishId!=null){
            //添加到购物车的是菜品
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else
        {
            //添加到购物车的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        if(cart!=null){
            //执行更新操作
            Integer number = cart.getNumber();
            cart.setNumber(number+1);
            shoppingCartService.updateById(cart);
        }else//执行插入操作
        {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cart=shoppingCart;
        }
        return R.success(cart);
    }

    /**
     * 查看购物车信息
     * @return 返回购物车信息
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //查询当前登录用户的id
        wrapper.eq(ShoppingCart::getUserId,Status.getCurrentId());
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        //查询
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 删除购物车信息
     * @return 返回删除信息
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //根据用户id删除购物车信息
        wrapper.eq(ShoppingCart::getUserId,Status.getCurrentId());
        boolean remove = shoppingCartService.remove(wrapper);
        if(remove){
            return R.success("删除成功！");
        }
        return R.error("删除失败！");
    }
    @PostMapping("/sub")
    public R<ShoppingCart> cleanById(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户的id
        Long userId = Status.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        //判断是菜品还是套餐
        if(shoppingCart.getDishId()!=null){
            //菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
            //查询要修改的菜品或套餐
            ShoppingCart cart = shoppingCartService.getOne(wrapper);
             Integer number = cart.getNumber();
        if(number==1){
            //库存未1直接清空
            shoppingCartService.remove(wrapper);
            return R.success(new ShoppingCart());
        }
            cart.setNumber(number-1);
            shoppingCartService.updateById(cart);
        ShoppingCart cart1 = shoppingCartService.getOne(wrapper);
        return R.success(cart1);
    }

}
